<template>
  <PageListContainer>
    <div class="base-block">
      <k-search :model="state.params" @search="methods.onSearch" @reset="methods.onReset">
        <el-form-item label="讲师搜索">
          <el-input v-model="state.params.keyword" clearable placeholder="请输入讲师账号名、手机号" style="width: 200px" />
        </el-form-item>
        <el-form-item label="加入时间">
          <el-date-picker
            v-model="state.params._dateRange"
            type="daterange"
            format="YYYY-MM-DD"
            value-format="YYYYMMDD"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :shortcuts="dateShortcut"
            @change="methods.onSearch"
          />
        </el-form-item>
      </k-search>
    </div>
    <div class="base-block">
      <KToolbar>
        <el-button type="primary" @click="onClickSendExchange">兑换码发放</el-button>
        <el-button type="primary" @click="onClickSendChannel">渠道码发放</el-button>
        <template #right>
          <el-button type="default" :icon="Download" @click="onClickExportData">导出数据</el-button>
        </template>
      </KToolbar>

      <KTable v-loading="state.loading" v-bind="tableState" v-on="tableEvent">
        <template #code_cnt="s">
          <el-link type="primary" @click="onClickCodeCnt(s.row)">{{ s.row.code_cnt }}</el-link>
        </template>
        <template #bind_cnt="s">
          <el-link type="primary" :disabled="!s.row.is_can_click" @click="onClickBindCnt(s.row)">{{ s.row.bind_cnt }}</el-link>
        </template>
        <template #cos_fee="s">
          <el-link type="primary" :disabled="!s.row.is_can_click" @click="onClickCosFee(s.row)">{{ s.row.cos_fee }}</el-link>
        </template>
        <template #actions="s">
          <el-link type="danger" @click="onClickClear(s.row)">清空</el-link>
        </template>
      </KTable>
    </div>
    <!-- 兑换码发放-->
    <ExchangeGrantModal ref="exchangeGrantModal" :list="tableState.data" @success="methods.onSearch" />
    <!-- 渠道码发放-->
    <ExchangeChannelModal ref="exchangeChannelModal" :list="tableState.data" @success="methods.onSearch" />
  </PageListContainer>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { useRouter } from 'vue-router';
import { Download } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import { useTableList } from './hooks/use-table';
import { lecturerListExport } from '@/api/crm-agency/download';
import { lecClear } from '@/api/crm-agency/dyj/lecturer';
import { baseConfirm } from '@crud/index';
import ExchangeGrantModal from './components/exchange-grant-modal.vue';
import ExchangeChannelModal from './components/exchange-channel-modal.vue';
import { AuthCode, hasPermission } from '@/modules/auth';
import { toolGetDateRangeShorcuts as dateShortcut } from '@/utils';

defineOptions({ name: 'AgencyDyjLecturerLecturerList' });
const { state, tableState, tableEvent, ...methods } = useTableList({});
const router = useRouter();
// 兑换码发放
const exchangeGrantModal = ref();
const onClickSendExchange = () => {
  if (hasPermission(AuthCode.AGENCY_LECTURER_GIFT_EXCHANGE_CODE)) {
    exchangeGrantModal.value.show(0);
  }
};
// 渠道码发放
const exchangeChannelModal = ref();
const onClickSendChannel = () => {
  exchangeChannelModal.value.show(0);
};
// 导出
const onClickExportData = () => {
  lecturerListExport(methods.getParams(state.params).data);
};
// 跳转
const onClickCodeCnt = (row: any) => {
  router.push(
    {
      name: 'AgencyDyjLecturerExchangeList',
      params: {
        agent_id: row.agent_id
      }
    }
  );
};
const onClickBindCnt = (row: any) => {
  router.push({
    name: 'AgencyDyjLecturerBindingList',
    params: {
      agent_id: row.agent_id
    }
  });
};
const onClickCosFee = (row: any) => {
  router.push({
    name: 'AgencyDyjLecturerCommissionList',
    params: {
      agent_id: row.agent_id
    }
  });
};
// 清空
const onClickClear = async (row: any) => {
  await baseConfirm({ title: '删除', message: '你确定删除？' });
  await lecClear({ agent_id: row.agent_id });
  ElMessage.success('清空成功');
  methods.getList();
};
</script>
